package jp.sinya.temicontact;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import jp.sinya.temicontact.mvp.model.Contact;
import jp.sinya.temicontact.mvp.presenter.ContactDetailPresenter;
import jp.sinya.temicontact.mvp.view.ContactDetailView;
import jp.sinya.temicontact.utils.GlideUtils;

/**
 * @author Koizumi Sinya
 * @date 2018/01/11. 21:16
 * @edithor
 * @date
 */
public class ContactDetailActivity extends AppCompatActivity implements ContactDetailView {

    private ImageView imageIcon;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvGender;
    private TextView tvEmail;
    private TextView tvAddress;

    private ContactDetailPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);

        init();

        Intent intent = getIntent();
        Contact contact = intent.getParcelableExtra("contact");

        presenter = new ContactDetailPresenter(this, contact);
        presenter.load();
    }

    private void init() {
        imageIcon = findViewById(R.id.image_icon);
        tvName = findViewById(R.id.tv_name);
        tvPhone = findViewById(R.id.tv_phone);
        tvGender = findViewById(R.id.tv_gender);
        tvEmail = findViewById(R.id.tv_email);
        tvAddress = findViewById(R.id.tv_address);
    }

    public void back(View view) {
        finish();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void closeLoading() {

    }

    @Override
    public void showError(String err) {

    }

    @Override
    public void loadData(Contact contact) {
        String[] urlParam = contact.getAvatar().split("\\?");
        GlideUtils.getInstance().loadCircularIcon(this, urlParam[0], R.mipmap.ic_launcher, imageIcon);
        tvName.setText(contact.getFirst_name() + " " + contact.getLast_name());
        tvPhone.setText(contact.getPhone());
        tvGender.setText(contact.getGender());
        tvEmail.setText(contact.getEmail());
        tvAddress.setText(contact.getAddress());
    }

    public void send(View view) {
        // Sinya: 2018/01/11 23:44 https://www.pubnub.com/
        String msg = "Hello " + (presenter.getContact() != null ? presenter.getContact().getFirst_name() : "");
        presenter.sendMessage(msg);
    }

}
